package com.lck.main.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

public class Server {
    public static void main(String[] args) {
//        s1();
        s2();
    }
    public static void s2(){
        try {
            System.out.println("服务端已经启动");
            ServerSocket serverSocket = new ServerSocket(8080);
            //保存连接的客户端socket对象
            ArrayList<Socket> clients = new ArrayList<>();
            //等待客户端连接
            while (true) {
                Socket accept  = serverSocket.accept();
                clients.add(accept);
                Scanner sc = new Scanner(System.in);
                System.out.println("有客人来了");
                InputStream is = accept.getInputStream();
//                OutputStream os = accept.getOutputStream();
                new Thread(()->{
                    while(true){
                        try{
                            byte[] buffer = new byte[1024];
                            int len = 0;
                            len = is.read(buffer);
                            String r = new String(buffer, 0, len);
                            System.out.println(r);
                            if (r.endsWith("bye")){
                                break;
                            }
                            for (Socket client : clients) {
                                OutputStream os = client.getOutputStream();
                                os.write(r.getBytes());
                            }
//                            String content = sc.nextLine();//获取控制台整行内容。
//                            content = "服务端:"+content;
//                            os.write(content.getBytes());
                        } catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("服务端启动已经完毕");
        }
    }
    public static void s1(){
        try {
            System.out.println("服务端已经启动");
            ServerSocket serverSocket = new ServerSocket(8080);
            //等待客户端连接
            while (true) {
                Socket accept  = serverSocket.accept();
                Scanner sc = new Scanner(System.in);
                System.out.println("有客人来了");
                InputStream is = accept.getInputStream();
                OutputStream os = accept.getOutputStream();
                new Thread(()->{
                    while(true){
                        try{
                            byte[] buffer = new byte[1024];
                            int len = 0;
                            len = is.read(buffer);
                            System.out.println(new String(buffer, 0, len));
                            if (new String(buffer, 0, len).endsWith("bye")){
                                break;
                            }
                            String content = sc.nextLine();//获取控制台整行内容。
                            content = "服务端:"+content;
                            os.write(content.getBytes());
                        } catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("服务端启动已经完毕");
        }
    }
}
